from . import user_blu
from flask import render_template, session, jsonify, request

from models import db
from models.index import Category, User


# 个人信息
@user_blu.route("/user_info", methods=["GET", "POST"])
def user_info():
    # 获取当前用户的信息
    user_id = session.get("user_id")

    # 查询用户头像
    user = db.session.query(User).filter(User.id == user_id).first()
    return render_template("user_info.html", user=user)


@user_blu.route("/user_info/user_info_detail", methods=["GET", "POST"])
def user_info_detail():
    # 获取用户的新的信息
    user_name = request.json.get("user_name")
    real_name = request.json.get("real_name")
    gender = request.json.get("gender")
    mobile = request.json.get("mobile")
    email = request.json.get("email")

    # 获取当前用户的信息
    user_id = session.get("user_id")
    print("------------------------>",user_id)
    # 储存到数据库
    user = db.session.query(User).filter(User.id == user_id).first()

    if not user:
        ret = {
            "errno": 4002,
            "errmsg": "没有此用户"
        }
        return jsonify(ret)
    # 如果查询到此用户就修改数据
    user.user_name = user_name
    user.real_name = real_name
    user.gender = gender
    user.mobile = mobile
    user.email = email

    db.session.commit()

    ret = {
        "errno": 0,
        "errmsg": "修改成功..."
    }
    return jsonify(ret)


# 修改密码
@user_blu.route("/user_Password")
def user_Password():
    # 获取当前用户的信息
    user_id = session.get("user_id")

    # 查询用户头像
    user = db.session.query(User).filter(User.id == user_id).first()
    return render_template("user_Password.html", user=user)


@user_blu.route("/user_Password/password_fix", methods=["GET", "POST"])
def user_password_detail():
    new_password = request.json.get("new_password")
    old_password = request.json.get("old_password")
    print("-------------", new_password, old_password)

    user_id = session.get("user_id")
    if not user_id:
        return jsonify({
            "errno": 4001,
            "errmsg": "请求登录"
        })

    user = db.session.query(User).filter(User.id == user_id, User.password == old_password).first()

    if user:
        user.password = new_password
        db.session.commit()
        ret = {
            "errno": 0,
            "errmsg": "修改成功"
        }
    else:
        ret = {
            "errno": 4004,
            "errmsg": "原密码错误!"
        }
    return jsonify(ret)


# 我的订单
@user_blu.route("/Orders")
def Orders():
    # 获取当前用户的信息
    user_id = session.get("user_id")

    # 查询用户头像
    user = db.session.query(User).filter(User.id == user_id).first()
    return render_template("Orders.html", user=user)


# 我的积分
@user_blu.route("/user_integral")
def user_integral():
    # 获取当前用户的信息
    user_id = session.get("user_id")

    # 查询用户头像
    user = db.session.query(User).filter(User.id == user_id).first()
    return render_template("user_integral.html", user=user)


# 我的收藏
@user_blu.route("/user_Collect")
def user_Collect():
    # 获取当前用户的信息
    user_id = session.get("user_id")

    # 查询用户头像
    user = db.session.query(User).filter(User.id == user_id).first()
    return render_template("user_Collect.html", user=user)


# 我的收货地址
@user_blu.route("/user_address")
def user_address():
    # 获取当前用户的信息
    user_id = session.get("user_id")

    # 查询用户头像
    user = db.session.query(User).filter(User.id == user_id).first()
    return render_template("user_address.html", user=user)
